from django.http import JsonResponse
from django.contrib.auth import authenticate, login, logout

def signin(request):
    userName = request.POST.get('username')
    passWord = request.POST.get('password')

    user = authenticate(username=userName, password=passWord)
    if user is not None:
        if user.is_active:
            if user.is_superuser:
                #登录方法
                login(request, user)
                #在session中存入用户类型
                request.session['usertype'] = 'mgr'
                return JsonResponse({'ret':0})
            else:
                return JsonResponse({'ret':1, 'msg':'请使用管理员账号登录'})
        else:
            return JsonResponse({'ret': 1, 'msg': '用户已被禁用'})
    else:
        return JsonResponse({'ret': 1, 'msg': '账号或密码错误'})

def signout(request):
    #登出方法
    logout(request)
    return JsonResponse({'ret':0})